package com.grj.community.all.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.grj.community.all.common.RedisKey;
import com.grj.community.all.common.Result;
import com.grj.community.all.entity.Follow;
import com.grj.community.all.entity.User;
import com.grj.community.all.service.FollowService;
import com.grj.community.all.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author 14505
 */
@RestController
@RequestMapping("/follow")
@RequiredArgsConstructor
public class FollowController {

    private final FollowService followService;
    private final HttpServletResponse httpServletResponse;
    private final UserService userService;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @GetMapping("/{id}")
    private Result<Object> follow(@PathVariable Long id){
        String uid = httpServletResponse.getHeader("uid");
        if(Long.valueOf(uid).equals( id)){
            return Result.success("不能关注自己");
        }
        Follow follow = new Follow();
        follow.setUid(Long.valueOf(uid));
        follow.setFid(id);
        Follow one = followService.getOne(new LambdaQueryWrapper<Follow>().eq(Follow::getUid, uid).eq(Follow::getFid, id));
        if(one == null){
            boolean save = followService.save(follow);
            if (save){
                redisTemplate.opsForSet().add(RedisKey.FOLLOW + uid, id);
                return Result.success("关注成功");
            }
        }
        followService.remove(new LambdaQueryWrapper<Follow>().eq(Follow::getUid, uid).eq(Follow::getFid, id));
        redisTemplate.opsForSet().remove(RedisKey.FOLLOW + uid, id);
        return Result.success("已取关");
    }

    @GetMapping("/common/{id}")
    private Result<Object> list(@PathVariable Long id){
        String uid = httpServletResponse.getHeader("uid");
        if(Long.valueOf(uid).equals( id)){
            return Result.success("不能关注自己");
        }
        Set<Object> intersect = redisTemplate.opsForSet().intersect(RedisKey.FOLLOW + uid, RedisKey.FOLLOW + id);
        if (intersect == null){
            return Result.success("没有共同关注");
        }
        List<Long> ids = intersect.stream().map(i -> Long.valueOf(i.toString())).collect(Collectors.toList());
        List<User> users = userService.listByIds(ids);
        List<Long> fids = followService.list(new LambdaQueryWrapper<Follow>().eq(Follow::getUid, uid)).stream().map(Follow::getFid).collect(Collectors.toList());
        System.out.println(fids);
        List<User> list = userService.listByIds(fids);
        Map<String, Object> map = new HashMap<>();
        map.put("common", users);
        map.put("me", list);
        return Result.success(map);
    }
}
